﻿using otis.entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace otis.views.project
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class ProjectList : Page
    {
        public ProjectList()
        {
            this.InitializeComponent();

            this.lvProvince.ItemsSource = this.InitProvinces();
            
            this.InitProjects();


            this.lvProvince.SelectedIndex = 0;
            this.lvProvince.SelectedItem = "北京";
        }


        private void LoadGridView(string provinceName)
        {
            var projects = new List<Project>();
            foreach (var item in this.InitProjects())
            {
                if (item == null)
                    continue;

                if (item.Province.Name.Equals(provinceName))
                {
                    projects.Add(item);
                }
            }

            this.gvDetails.ItemsSource = projects;
        }


        private List<Province> InitProvinces()
        {
            var provinces = new List<Province>();
            provinces.Add(new Province() { Id = 1, Name = "北京" });
            provinces.Add(new Province() { Id = 2, Name = "上海" });
            provinces.Add(new Province() { Id = 3, Name = "广东" });
            provinces.Add(new Province() { Id = 4, Name = "天津" });

            return provinces;
        }


        private List<Project> InitProjects()
        {
            var provinces = this.InitProvinces();

            var projects = new List<Project>();
            projects.Add(new Project() { Id = 1, Name = "北京公安部", ImagePath = "/Assets/project/01.jpg", Province = provinces[0] });
            projects.Add(new Project() { Id = 2, Name = "北京铁道部", ImagePath = "/Assets/project/02.jpg", Province = provinces[0] });
            projects.Add(new Project() { Id = 3, Name = "北京交通部", ImagePath = "/Assets/project/03.jpg", Province = provinces[0] });
            projects.Add(new Project() { Id = 4, Name = "北京外交部", ImagePath = "/Assets/project/04.jpg", Province = provinces[0] });
            projects.Add(new Project() { Id = 5, Name = "国务院", ImagePath = "/Assets/project/02.jpg", Province = provinces[0] });
            projects.Add(new Project() { Id = 6, Name = "中关村大厦", ImagePath = "/Assets/project/05.jpg", Province = provinces[0] });
            projects.Add(new Project() { Id = 6, Name = "新东方", ImagePath = "/Assets/project/01.jpg", Province = provinces[0] });

            projects.Add(new Project() { Id = 6, Name = "1111111", ImagePath = "/Assets/project/01.jpg", Province = provinces[1] });
            projects.Add(new Project() { Id = 6, Name = "2222222", ImagePath = "/Assets/project/02.jpg", Province = provinces[1] });

            return projects;

        }

        private void lvProvince_ItemClick(object sender, ItemClickEventArgs e)
        {
            Province item = e.ClickedItem as Province;
            if (item != null)
            {
                this.LoadGridView(item.Name);
            }
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }
    }
}
